<?php
/**
 * 员工转正邮件提醒
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Tools_NewGuys');
apf_require_class('Bll_Hr_OutInterface');

class Oa_Job_Flow_ToPositive extends Oa_Job_Abstract {
    private $read_db = "hroa_slave";
    private $write_db = "hroa_master";
    private $retry_num = 10;

    public function run() {
        $this->log("员工转正邮件--开始 ".date('Y-m-d H:i:s'));
        /**
         * 从user表里面找到今天转正的同学
         */
        $today = date('Ymd',time());
        $positive = array();
        $sql = "SELECT user_id,real_name,office_mail FROM user WHERE indue_date = ?";
        $positive = $this->get_db_rows($sql, $this->read_db, array($today));

        if (empty($positive)) {
                exit();
        }
        
        $this->_send_mail($positive);
        $failed_mail = json_encode($positive);
        $this->log("员工转正邮件--结束,没有发送成功的有".$failed_mail."   ".date('Y-m-d H:i:s'));
    }

    private function _send_mail(&$arr) {
        for ($i=1;$i<=$this->retry_num;$i++) {
            if (!empty($arr)) {
                foreach ($arr as $k=>$val) {
                    if (Tools_NewGuys::get_instance()->check_mail($val['office_mail'])) {
                        $content = self::_build_mail_template($val['real_name']);
                        $hr_bll = new Bll_Hr_OutInterface();
                        //获得上级主管
                        $super_data = $hr_bll->get_under_leader($val['user_id']);
                        $result = Tools_NewGuys::get_instance()->send($val['office_mail'],$super_data['mail'],'恭喜你成功转正',$content);
                        if ($result == 1) {
                            unset($arr[$k]);
                        }
                    }
                }
            } else {
                break;
            }
        }
        return $arr;
    }

    static function _build_mail_template($user) {
        /**
         * 蛋疼的做法
         * 因为邮件客户端不统一，发的邮件格式有问题
         * 所以统一生成图片之后发送
         * 蛋疼吧？？？哈哈哈
         */
        global $G_STATIC_PATH;
        $imgSrc = "$G_STATIC_PATH[0]pass.jpg";
        $im = new Imagick($imgSrc);

        $width = $im->getImageWidth();
        $height = $im->getImageHeight();

        $N_Width = 960;
        $N_Height = (int)($height / $width * $N_Width);

        //裁剪
        $im->resizeImage($N_Width,$N_Height,Imagick::FILTER_LANCZOS, 1, true);

        $year = date('Y',time());
        $month = date('m',time());
        $day = date('d',time());

        //打字
        $draw = new ImagickDraw();
        $draw->setFont("$G_STATIC_PATH[0]msyh.ttf");
        $draw->setFontSize(30);
        $draw->setFillColor(new ImagickPixel('#60482d'));
        $im->annotateImage($draw, 145, 165, 0, $user);
        //正文日期
        $draw->setFontSize(25);
        $im->annotateImage($draw, 670, 296, 0, $year);
        $im->annotateImage($draw, 767, 296, 0, $month);
        $im->annotateImage($draw, 845, 296, 0, $day);
        //签名档日期
        $draw->setFontSize(22);
        $im->annotateImage($draw, 755, 570, 0, $year);
        $im->annotateImage($draw, 850, 570, 0, $month);

        $im->setImageFormat("JPEG");
        //压缩
        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
        $im->setImageCompressionQuality(80);
        $im->stripImage();
        $im->setImageColorspace(Imagick::COLORSPACE_RGB);
         
        
        $file_name = md5($user.time()).".jpg";

        $im->writeImage("$G_STATIC_PATH[0]indue/$file_name");

        $im->clear();
        $im->destroy();
        unset($im);

        //$result = '<html><body><div><img src="data:image/jpeg;base64,'.$image_data.'" /></div></body></html>';
        //$result = '<html><body><table><tr><td style="background: #ffffff url(http://192.168.1.191:8066/indue/'.$file_name.') no-repeat fixed top;">dwdwdew</td></tr></table></body></html>';


        $result='<html><body><img src="'.$G_STATIC_PATH[0].'indue/'.$file_name.'" alt="你好，我们很荣幸的通知你，你经过了试用期的考核，我们认为你很适合当前所在的职位，希望你在今后的日子，再接再厉，创造更多的奇迹，我们对你很期待。" /></body></html>';
        //$result = '<html><body><table style="width:960px;background:#ffffff url('.$G_STATIC_PATH[0].'indue/'.$file_name.')"><div>你好，我们很荣幸的通知你，你已经成功转正成为安居客正式员工了。</div></table></body></html>';
        
        return $result;
    }
}
